﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.IO;

namespace DataAccess
{
    public class ProductRepository : ConnectionClass
    {
        public ProductRepository()
            : base()
        {

        }


        public void AddProduct( Product p)
        {
            db.AddToProducts(p);
            db.SaveChanges();
        }

        public void UpdateProduct(Product p)
        {
            Product OP = GetProductByID(p.ID);
            db.Products.Attach(OP);
            db.Products.ApplyCurrentValues(p);
            db.SaveChanges();
        }

        public Product GetProductByID(int id)
        {
            return db.Products.SingleOrDefault(p => p.ID == id);
        }

        public IEnumerable<Product> GetAllProducts()
        {
            return db.Products;
        }

        public IEnumerable<Product> SearhProduct(int category, string name, int LeastPrice, int MaxPrice)
        {
            List<Product> foundProducts = new List<Product>();
            foreach (Product p in db.Products)
            {
                if (p.Price > LeastPrice && p.Price < MaxPrice)
                {
                    if ((p.Name == name || name == "") && (p.CategoryID == category || category == 0))
                        foundProducts.Add(p);
                }

                if(LeastPrice == 0 && MaxPrice == 0)
                {
                    if ((p.Name.ToLower().Contains(name) || p.Name.ToUpper().Contains(name) || name == "") && (p.CategoryID == category || category == 0))
                        foundProducts.Add(p);
                }
            }

            return foundProducts.AsEnumerable();
        }

        public IEnumerable<Category> GetAllCategories()
        {
            return db.Categories;
        }

        public IEnumerable<Product> GetAllUserProducts(string username)
        {
            return (from O in db.Orders
                    join OD in db.OrderDetails on O.OrderID equals OD.OrderID
                    join p in db.Products on OD.ProductID equals p.ID
                    where O.UserName == username
                    select p);
        }

        public IEnumerable<Common.Views.UserItem> GetUserProducts(string username)
        {
            IEnumerable<Common.Views.UserItem> products;
            products = (from OD in db.OrderDetails
                        join O in db.Orders on OD.OrderID equals O.OrderID
                        join p in db.Products on OD.ProductID equals p.ID
                        where O.UserName == username
                        select new Common.Views.UserItem
                        {
                            ID = p.ID,
                            PurchaseID = O.OrderID,
                            ProductName = p.Name,
                            Quantity = (int)OD.Quantity,
                            DatePurchased = (DateTime)O.Date,
                            WarranyExpires = (DateTime)O.Date,
                            AmountPurchased = (double)p.Price,
                            Features = p.Description,

                        });
            int test = products.Count();
            return products;

        }

    }
}
